Option Explicit

Private Const TEST_TABLE_NAME As String = "table1"
Private Const TEST_POINT As String = "."
Private Const TEST_COL_NAME As String = "col1"
Private Const TEST_SHARE_LINE_COMMENT As String = "--aaaaaa"
Private Const TEST_SINGLE_LINE_COMMENT As String = "--bbbbb"

' ======================================================================
' 执行所有测试用例
' ======================================================================
Public Sub test_all_case()
    ' ==================================================
    ' ISqlFormatterElem 接口实现测试
    ' ==================================================
    ' 测试: addComponent 方法
    test_ISqlFormatterElem_addComponent
    ' 测试: addComponentToFirst 方法
    test_ISqlFormatterElem_addComponentToFirst
    ' 测试: canAddComponentToFirst 方法
    test_ISqlFormatterElem_canAddComponentToFirst
    ' 测试: getAlignmentElemGroups 方法
    test_ISqlFormatterElem_getAlignmentElemGroups
    ' 测试: setElemNestCount 方法
    test_ISqlFormatterElem_setElemNestCount
    ' 测试: setPrefixSpaceCount 方法
    test_ISqlFormatterElem_setPrefixSpaceCount
    ' 测试: hasBoundaryParenthesis 方法
    test_ISqlFormatterElem_hasBoundaryParenthesis
    ' 测试: addBoundaryLeftParenthesis 方法
    test_ISqlFormatterElem_addBoundaryLeftParenthesis
    ' 测试: addBoundaryRightParenthesis 方法
    test_ISqlFormatterElem_addBoundaryRightParenthesis
    ' 测试: getType 方法
    test_ISqlFormatterElem_getType
    ' 测试: isComment 方法
    test_ISqlFormatterElem_isComment
    ' 测试: isShareLineComment 方法
    test_ISqlFormatterElem_isShareLineComment
    ' 测试: isSingleLineComment 方法
    test_ISqlFormatterElem_isSingleLineComment
    ' 测试: isEndsWithComment 方法
    test_ISqlFormatterElem_isEndsWithComment
    ' 测试: isMultiLine 方法
    test_ISqlFormatterElem_isMultiLine
    ' 测试: toString + toStringArray 方法
    test_ISqlFormatterElem_toString_toStringArray
End Sub


' ======================================================================
' 测试通用处理
' ======================================================================
' 获取表名 token
Private Function getTableNameToken() As SqlToken
    Set getTableNameToken = New SqlToken
    getTableNameToken.Init TEST_TABLE_NAME, SqlTokenType.STR_NAME
End Function

' 获取 [.] token
Private Function getPointToken() As SqlToken
    Set getPointToken = New SqlToken
    getPointToken.Init TEST_POINT, SqlTokenType.POINT
End Function

' 获取列名 token
Private Function getColNameToken() As SqlToken
    Set getColNameToken = New SqlToken
    getColNameToken.Init TEST_COL_NAME, SqlTokenType.STR_NAME
End Function

' 获取和前一个元素在同一行的注释
Private Function getShareLineCommentToken() As SqlToken
    Set getShareLineCommentToken = New SqlToken
    getShareLineCommentToken.Init TEST_SHARE_LINE_COMMENT, SqlTokenType.SHARE_LINE_COMMENT
End Function

' 获取一个单行的注释
Private Function getSingleLineCommentToken() As SqlToken
    Set getSingleLineCommentToken = New SqlToken
    getSingleLineCommentToken.Init TEST_SINGLE_LINE_COMMENT, SqlTokenType.SINGLE_LINE_COMMENT
End Function

' ======================================================================
' ISqlFormatterElem 接口实现测试
' ======================================================================
' 测试: addComponent 方法
Private Sub test_ISqlFormatterElem_addComponent()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    ' ==============================
    ' 2. 添加第一个元素
    ' ==============================
    elem.addComponent getTableNameToken
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = TEST_TABLE_NAME
End Sub

' 测试: addComponentToFirst 方法
Private Sub test_ISqlFormatterElem_addComponentToFirst()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    ' ==============================
    ' 2. 添加第一个元素
    ' ==============================
    elem.addComponent getTableNameToken
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = TEST_TABLE_NAME

    ' ==============================
    ' 3. 禁止从左侧添加
    ' ==============================
    elem.addComponentToFirst getPointToken
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = TEST_TABLE_NAME
End Sub

' 测试: canAddComponentToFirst 方法
Private Sub test_ISqlFormatterElem_canAddComponentToFirst()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' ==============================
    ' 2. 默认为 false
    ' ==============================
    Debug.Assert elem.canAddComponentToFirst = False
End Sub

' 测试: getAlignmentElemGroups 方法
Private Sub test_ISqlFormatterElem_getAlignmentElemGroups()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' ==============================
    ' 2. 在添加元素之前进行检查
    ' ==============================
    Debug.Assert elem.getAlignmentElemGroups Is Nothing

    ' ==============================
    ' 3. 添加第一个元素后再检查
    ' ==============================
    elem.addComponent getTableNameToken

    Debug.Assert elem.getAlignmentElemGroups Is Nothing
End Sub

' 测试: setElemNestCount 方法
Private Sub test_ISqlFormatterElem_setElemNestCount()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    ' ==============================
    ' 2. 执行方法，然后添加一个元素
    ' ==============================
    elem.setElemNestCount 20, config

    elem.addComponent getTableNameToken
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME
End Sub

' 测试: setPrefixSpaceCount 方法
Private Sub test_ISqlFormatterElem_setPrefixSpaceCount()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    ' ==============================
    ' 2. 执行方法，然后添加一个元素
    ' ==============================
    elem.setPrefixSpaceCount 22, config

    elem.addComponent getTableNameToken
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME
End Sub

' 测试: hasBoundaryParenthesis 方法
Private Sub test_ISqlFormatterElem_hasBoundaryParenthesis()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' ==============================
    ' 2. 默认为 true
    ' ==============================
    Debug.Assert elem.hasBoundaryParenthesis = True
End Sub

' 测试: addBoundaryLeftParenthesis 方法
Private Sub test_ISqlFormatterElem_addBoundaryLeftParenthesis()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    ' ==============================
    ' 2. 添加一个元素
    ' ==============================
    elem.addComponent getTableNameToken
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME

    ' ==============================
    ' 3. 添加一个左括号
    ' ==============================
    elem.addBoundaryLeftParenthesis
    Debug.Assert elem.toString(config) = "(" & TEST_TABLE_NAME

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "(" & TEST_TABLE_NAME

    ' ==============================
    ' 4. 添加多个左括号
    ' ==============================
    elem.addBoundaryLeftParenthesis(3)
    Debug.Assert elem.toString(config) = "((((" & TEST_TABLE_NAME

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "((((" & TEST_TABLE_NAME
End Sub

' 测试: addBoundaryRightParenthesis 方法
Private Sub test_ISqlFormatterElem_addBoundaryRightParenthesis()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    ' ==============================
    ' 2. 添加一个元素
    ' ==============================
    elem.addComponent getTableNameToken
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME

    ' ==============================
    ' 3. 添加一个右括号
    ' ==============================
    elem.addBoundaryRightParenthesis
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME & ")"

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = TEST_TABLE_NAME & ")"

    ' ==============================
    ' 4. 添加多个右括号
    ' ==============================
    elem.addBoundaryRightParenthesis(3)
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME & "))))"

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = TEST_TABLE_NAME & "))))"
End Sub

' 测试: getType 方法
Private Sub test_ISqlFormatterElem_getType()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    ' ==============================
    ' 2. 默认没有类型
    ' ==============================
    Debug.Assert elem.getType = SqlTokenType.NO_MEAN

    ' ==============================
    ' 3. 添加一个元素后，也没有类型
    ' ==============================
    elem.addComponent getTableNameToken
    Debug.Assert elem.getType = SqlTokenType.NO_MEAN
End Sub

' 测试: isComment 方法
Private Sub test_ISqlFormatterElem_isComment()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g
    ' ==============================
    ' 2. 默认为 false
    ' ==============================
    Debug.Assert elem.isComment = False

    ' ==============================
    ' 3. 即使添加一个注释，也是 false
    ' ==============================
    elem.addComponent getShareLineCommentToken
    Debug.Assert elem.isComment = False

    elem.addComponent getSingleLineCommentToken
    Debug.Assert elem.isComment = False
End Sub

' 测试: isShareLineComment 方法
Private Sub test_ISqlFormatterElem_isShareLineComment()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' ==============================
    ' 2. 默认为 false
    ' ==============================
    Debug.Assert elem.isShareLineComment = False

    ' ==============================
    ' 3. 即使添加一个注释，也是 false
    ' ==============================
    elem.addComponent getShareLineCommentToken
    Debug.Assert elem.isShareLineComment = False

    elem.addComponent getSingleLineCommentToken
    Debug.Assert elem.isShareLineComment = False
End Sub

' 测试: isSingleLineComment 方法
Private Sub test_ISqlFormatterElem_isSingleLineComment()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' ==============================
    ' 2. 默认为 false
    ' ==============================
    Debug.Assert elem.isShareLineComment = False

    ' ==============================
    ' 3. 即使添加一个注释，也是 false
    ' ==============================
    elem.addComponent getShareLineCommentToken
    Debug.Assert elem.isSingleLineComment = False

    elem.addComponent getSingleLineCommentToken
    Debug.Assert elem.isSingleLineComment = False
End Sub

' 测试: isEndsWithComment 方法
Private Sub test_ISqlFormatterElem_isEndsWithComment()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' ==============================
    ' 2. 默认为 false
    ' ==============================
    Debug.Assert elem.isEndsWithComment = False

    ' ==============================
    ' 3. 即使添加一个注释，也是 false
    ' ==============================
    elem.addComponent getShareLineCommentToken
    Debug.Assert elem.isEndsWithComment = False

    elem.addComponent getSingleLineCommentToken
    Debug.Assert elem.isEndsWithComment = False
End Sub

' 测试: isMultiLine 方法
Private Sub test_ISqlFormatterElem_isMultiLine()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' ==============================
    ' 2. 默认为 false
    ' ==============================
    Debug.Assert elem.isMultiLine = False

    ' ==============================
    ' 3. 即使添加一个注释，也是 false
    ' ==============================
    elem.addComponent getShareLineCommentToken
    Debug.Assert elem.isMultiLine = False

    elem.addComponent getSingleLineCommentToken
    Debug.Assert elem.isMultiLine = False
End Sub

' 测试: toString + toStringArray 方法
Private Sub test_ISqlFormatterElem_toString_toStringArray()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterContinuousStrElem
    Set g = New SqlFormatterContinuousStrElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    ' ==============================
    ' 2. 添加第一个元素
    ' ==============================
    elem.addComponent getTableNameToken
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = TEST_TABLE_NAME

    ' ==============================
    ' 3. 添加第二个元素
    ' ==============================
    elem.addComponent getPointToken
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME & TEST_POINT

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = TEST_TABLE_NAME & TEST_POINT

    ' ==============================
    ' 4. 添加第三个元素
    ' ==============================
    elem.addComponent getColNameToken
    Debug.Assert elem.toString(config) = TEST_TABLE_NAME & TEST_POINT & TEST_COL_NAME

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = TEST_TABLE_NAME & TEST_POINT & TEST_COL_NAME

    ' ==============================
    ' 5. 添加边界括号
    ' ==============================
    elem.addBoundaryLeftParenthesis(2)
    elem.addBoundaryRightParenthesis(2)
    Debug.Assert elem.toString(config) = "((" & TEST_TABLE_NAME & TEST_POINT & TEST_COL_NAME & "))"

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "((" & TEST_TABLE_NAME & TEST_POINT & TEST_COL_NAME & "))"

    ' ==============================
    ' 6. 测试空对象
    ' ==============================
    Set g = New SqlFormatterContinuousStrElem

    Debug.Assert g.ISqlFormatterElem_toString(config) = ""

End Sub